package com.team.dutchboy.config.seata;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

//解决sleuth,seata,sentinel bean冲突问题  手动将请求拦截，传递seata的xid
@Component
@ConditionalOnClass({RequestInterceptor.class, GlobalTransactional.class})
public class SeataInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        String currentXid = RootContext.getXID();
        if (StringUtils.isNotEmpty(currentXid)) {
            requestTemplate.header(RootContext.KEY_XID, currentXid);
        }
    }
}
